package com.control.qct.businesssteward.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.OrderDetailAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.DoPayGoodsModel;
import com.control.qct.businesssteward.model.OrderDetailModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 订单详情
 * Created by Administrator on 2018/1/9 0009.
 */

public class OrderDetailActivity extends BaseActivity {
    @BindView(R.id.ll_order_detail)
    LinearLayout mLlOrderDetail;
    @BindView(R.id.tv_order_code)
    TextView mTvOrderCode;//订单号
    @BindView(R.id.tv_order_time)
    TextView mTvOrderTime;//下单时间
    @BindView(R.id.tv_pay_style)
    TextView mTvPayStyle;//支付方式
    @BindView(R.id.tv_order_status)
    TextView mTvOrderStatus;//订单状态
    @BindView(R.id.tv_cashier)
    TextView mTvCashier;//收银员
    @BindView(R.id.tv_guider)
    TextView mTvGuider;//导购员
    @BindView(R.id.rv_goods_detail)
    RecyclerView mRvGoodsDetail;//订单详情
    @BindView(R.id.tv_all_goods_price)
    TextView mTvAllGoodsPrice;//商品总额
    @BindView(R.id.tv_order_discounts)
    TextView mTvOrderDiscounts;//优惠折扣
    @BindView(R.id.tv_return_money)
    TextView mTvReturnMoney;//返现
    @BindView(R.id.tv_practical_receipts_status)
    TextView mTvPracticalReceiptsStatus;
    @BindView(R.id.tv_practical_receipts_money)
    TextView mTvPracticalReceiptsMoney;//实收款金额
    @BindView(R.id.tv_gathering)
    TextView mTvGathering;//收款
    @BindView(R.id.rl_gathering)
    RelativeLayout mRlGathering;
    @BindView(R.id.tv_sure_chargeback)
    TextView mTvSureChargeback;
    @BindView(R.id.ll_sure_chargeback)
    LinearLayout mLvSureChargeback;

    private List<OrderDetailModel.DetailsBean> mOrderDetailModelList;
    private String mOrderSn;
    private Bundle mBundle;
    private OrderDetailAdapter mOrderDetailAdapter;
    private DoPayGoodsModel mDoPayGoodsModel;
    private static final int REQUEST_ORDER_DETAIL_RESULT = 10011;


    public OrderDetailActivity() {
        super(R.layout.activity_order_detail);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mOrderSn = extras.getString("orderSn");
        mDoPayGoodsModel = (DoPayGoodsModel) extras.getSerializable("order_information");
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("订单详情");
        mBundle = new Bundle();
        mBundle.putInt("id", 0);

        getSaleOrderDetail();
        mRvGoodsDetail.setLayoutManager(new LinearLayoutManager(this));
        mOrderDetailAdapter = new OrderDetailAdapter(mOrderDetailModelList);
        mRvGoodsDetail.setAdapter(mOrderDetailAdapter);
    }

    private void getSaleOrderDetail() {
        mOrderDetailModelList = new ArrayList<>();
        mNetProtocol.getSaleOrderDetail(this, mOrderSn, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mLlOrderDetail.setVisibility(View.VISIBLE);
                OrderDetailModel orderDetailModel = (OrderDetailModel) baseBean.getData();
                mTvOrderCode.setText("订单号：" + orderDetailModel.getOrderSN());
                mTvOrderTime.setText("下单时间：" + orderDetailModel.getCreateDT());
                StringBuilder payStyle = new StringBuilder();
                for (int i = 0; i < orderDetailModel.getPayways().size(); i++) {
                    payStyle.append(orderDetailModel.getPayways().get(i));
                }
                mTvPayStyle.setText("支付方式：" + payStyle.toString());
                mTvOrderStatus.setText(orderDetailModel.getState());
                mTvCashier.setText("收银员：" + orderDetailModel.getCashier());
                mOrderDetailModelList.addAll(orderDetailModel.getDetails());
                if (mOrderDetailAdapter != null) {
                    mOrderDetailAdapter.notifyDataSetChanged();
                }
                mTvAllGoodsPrice.setText(DecimalFormatUtils.DecimalFormatTwo(orderDetailModel.getTotalAmount()));
                mTvOrderDiscounts.setText(DecimalFormatUtils.DecimalFormatTwo(orderDetailModel.getOrderDiscount()));
                mTvReturnMoney.setText(DecimalFormatUtils.DecimalFormatTwo(orderDetailModel.getPreferentialSubTotal()));
                mTvPracticalReceiptsMoney.setText(DecimalFormatUtils.DecimalFormatTwo(orderDetailModel.getReceive()));
                mRlGathering.setVisibility(orderDetailModel.getState().equals("待收款") ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }


    @OnClick({R.id.tv_gathering, R.id.tv_sure_chargeback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_gathering:
                if (mSPUtils.getObject(mNetProtocol.getUserCode() + "_store") == null) {
                    showShortToast("未选择门店，请先选择门店！");
                    return;
                }
                mBundle.putInt("id", 1);
                mBundle.putSerializable("order_information", mDoPayGoodsModel);
                startActivityForResult(PaymentActivity.class, REQUEST_ORDER_DETAIL_RESULT, mBundle);
                break;
            case R.id.tv_sure_chargeback:
//                mBundle.putInt("id", 0);
                startActivity(ChargebackStatusActivity.class, mBundle);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            getSaleOrderDetail();
            setResult(RESULT_OK);
        }
    }
}
